import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * lovSetting - 值集视图配置 - model
 * @date: 2018-6-26

 */
import { getResponse, createPagination } from 'utils/utils';
import { queryIdpValue } from 'services/api';
import { queryLovList, deleteLovValue as _deleteLovValue, addLovValue as _addLovValue, queryHeadValue, queryLineValue, addLineData, editLine, removeLine, updateHeadValue, copyLovView as _copyLovView } from "../services/lovService";
export default {
  namespace: 'lovSetting',
  state: {
    list: {},
    pagination: {},
    headData: {},
    rowData: {
      list: [],
      pagination: {}
    },
    mainKey: {
      viewHeaderId: '',
      lovId: ''
    },
    dataTypeList: []
  },
  effects: {
    queryValueList: /*#__PURE__*/_regeneratorRuntime.mark(function queryValueList(_, _ref) {
      var put, call, res;
      return _regeneratorRuntime.wrap(function queryValueList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              put = _ref.put, call = _ref.call;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CPFM.VIEW.DATA_TYPE');

            case 4:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);

              if (!res) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  dataTypeList: res
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, queryValueList);
    }),
    fetchLovList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLovList(_ref2, _ref3) {
      var payload, call, put, response, list;
      return _regeneratorRuntime.wrap(function fetchLovList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(queryLovList, payload);

            case 4:
              response = _context2.sent;
              list = getResponse(response);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'query',
                payload: list
              });

            case 9:
              return _context2.abrupt("return", list);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchLovList);
    }),
    addLovValue: /*#__PURE__*/_regeneratorRuntime.mark(function addLovValue(_ref4, _ref5) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function addLovValue$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_addLovValue, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, addLovValue);
    }),
    deleteLovValue: /*#__PURE__*/_regeneratorRuntime.mark(function deleteLovValue(_ref6, _ref7) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function deleteLovValue$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_deleteLovValue, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, deleteLovValue);
    }),
    fetchHead: /*#__PURE__*/_regeneratorRuntime.mark(function fetchHead(_ref8, _ref9) {
      var payload, call, put, response;
      return _regeneratorRuntime.wrap(function fetchHead$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              _context5.next = 4;
              return call(queryHeadValue, payload);

            case 4:
              response = _context5.sent;
              _context5.next = 7;
              return put({
                type: 'queryHead',
                payload: response
              });

            case 7:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchHead);
    }),
    fetchLine: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLine(_ref10, _ref11) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchLine$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call, put = _ref11.put;
              _context6.next = 4;
              return call(queryLineValue, payload);

            case 4:
              response = _context6.sent;
              data = getResponse(response);

              if (!data) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'queryLine',
                payload: data
              });

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchLine);
    }),
    saveHead: /*#__PURE__*/_regeneratorRuntime.mark(function saveHead(_ref12, _ref13) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function saveHead$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(updateHeadValue, payload);

            case 4:
              response = _context7.sent;
              return _context7.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, saveHead);
    }),
    addLine: /*#__PURE__*/_regeneratorRuntime.mark(function addLine(_ref14, _ref15) {
      var payload, callback, call, response;
      return _regeneratorRuntime.wrap(function addLine$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload, callback = _ref14.callback;
              call = _ref15.call;
              _context8.next = 4;
              return call(addLineData, payload);

            case 4:
              response = _context8.sent;
              if (response && callback) callback();

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, addLine);
    }),
    editLineData: /*#__PURE__*/_regeneratorRuntime.mark(function editLineData(_ref16, _ref17) {
      var payload, callback, call, response;
      return _regeneratorRuntime.wrap(function editLineData$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload, callback = _ref16.callback;
              call = _ref17.call;
              _context9.next = 4;
              return call(editLine, payload);

            case 4:
              response = _context9.sent;
              if (response && callback) callback();

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, editLineData);
    }),
    // 删除行数据
    removeLineData: /*#__PURE__*/_regeneratorRuntime.mark(function removeLineData(_ref18, _ref19) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function removeLineData$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref18.payload;
              call = _ref19.call;
              _context10.next = 4;
              return call(removeLine, payload);

            case 4:
              response = _context10.sent;
              return _context10.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, removeLineData);
    }),
    // 复制值集视图
    copyLovView: /*#__PURE__*/_regeneratorRuntime.mark(function copyLovView(_ref20, _ref21) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function copyLovView$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref20.payload;
              call = _ref21.call;
              _context11.next = 4;
              return call(_copyLovView, payload);

            case 4:
              res = _context11.sent;
              return _context11.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, copyLovView);
    })
  },
  reducers: {
    query: function query(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        list: action.payload,
        pagination: createPagination(action.payload)
      });
    },
    queryHead: function queryHead(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        headData: action.payload,
        mainKey: {
          viewHeaderId: action.payload.viewHeaderId,
          lovId: action.payload.lovId
        }
      });
    },
    queryLine: function queryLine(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        rowData: _objectSpread({
          list: action.payload.content,
          pagination: createPagination(action.payload)
        }, action.payload)
      });
    },
    updateState: function updateState(state, _ref22) {
      var payload = _ref22.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};